import { ManageOrganization } from './../../interface/interface';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { UserManagementService } from '../../user-management.service';
import { ToolsService, NotificationService } from 'app/shared/service';

@Component({
    selector: 'itmp-add-organization',
    templateUrl: './add-organization.component.html',
    styleUrls: ['./add-organization.component.css']
})
export class AddOrganizationComponent implements OnInit {
    isOkLoading = false;
    data = '';
    // @Input() selectionList: any;
    @Input() visible = false;
    @Input() title = '';
    @Input() userId = '';
    @Input() selectionList: any;
    @Input() listOfOption: any;
    @Output() visibleChange = new EventEmitter<boolean>();
    @Output() manageUserChange = new EventEmitter<void>();

    // 初始化表单
    @Input()
    set manageUser(value: ManageOrganization) {
        if (value) {
            this.formDataInit(value);
        }
    }
    @Input() roleOptions = [];
    userInfoForm: FormGroup;
    constructor(
        private fb: FormBuilder,
        private http: HttpClient,
        private UMService: UserManagementService,
        private tools: ToolsService,
        private msg: NotificationService
    ) {
        this.userInfoForm = this.fb.group({
            organization_Id: 0,
            organization_Name: [null, Validators.required]
        });
    }
    private formDataInit(value: ManageOrganization) {
        this.userInfoForm.setValue(value);
    }

    handleCancel() {
        this.close();
    }
    close() {
        this.visible = false;
        this.visibleChange.emit(this.visible);
    }

    // 模态框点击确定按钮
    submitForm(): void {
        // tslint:disable-next-line:forin
        for (const i in this.userInfoForm.controls) {
            if (this.userInfoForm.controls.hasOwnProperty(i)) {
                this.userInfoForm.controls[i].markAsDirty();
                this.userInfoForm.controls[i].updateValueAndValidity();
            }
        }

        setTimeout(() => {
            if (value.organization_Name && value.organization_Name.length !== 0) {
                if (value.organization_Id === 0) {
                    // 新建
                    this.UMService.createOrganization(value).subscribe(res => {
                        if (res.code === '000000') {
                            this.manageUserChange.emit();
                            this.visible = false;
                            this.visibleChange.emit(this.visible);
                            this.isOkLoading = false;
                        } else if (res.code === '900013') {
                            this.msg.notify({ type: 'warning', path: 'UserManagement.ManageOrganization.createRepeat_notify' });
                            this.visible = true;
                        }
                    });
                } else {
                    this.UMService.updateOrganization(value).subscribe(res => {
                        this.manageUserChange.emit();
                    });
                }
            }
        }, 100);
        const value = this.tools.isClone(this.userInfoForm.value);
    }

    ngOnInit() {}
}
